#Packages
library(lfe)
library(stargazer)
library(xtable)
library(plyr)

#Data Frame
load('cces.congruence.combined.RData')

#######
###Outcomes By Time Period
#######
ddply(final.combined,.(year.bin.3),summarise,rep.approval.place=mean(rep.approval.place,na.rm=TRUE),rep.incumbent.ideology=mean(rep.incumbent.ideology,na.rm=TRUE),rep.party.place=mean(rep.party.place,na.rm=TRUE), rep.intent.place=mean(rep.intent.place,na.rm=TRUE), voted.for.rep=mean(voted.for.rep,na.rm=TRUE))

#######
###Placebo Analysis
#######
congress.knowledge <- felm(house.majority.correct ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined)

congress.approval <- felm(congress.institutional.approval ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined)

sen.approval <- felm(I(sen1.approval.place+sen2.approval.place==2) ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined)

gov.approval <- felm(gov.approval.place ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education)  + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined)

pres.approval <- felm(pres.approval.place ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined)

stargazer(congress.knowledge,congress.approval,sen.approval,gov.approval,pres.approval,keep=c('congruence'),dep.var.labels=c('Knows Majority','Congress Place','Senator Place','Governor Place','President Place'),covariate.labels=c('Congruence'),notes=c('Robust Standard Errors, Clustered by County-District'),star.cutoffs=c(0.05),notes.align='l')

#######
###Main Effects
#######
rep.approval <- felm(rep.approval.place ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education)  + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined)

rep.ideology <- felm(rep.incumbent.ideology ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education)  + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined)

rep.party <- felm(rep.party.place ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education)  + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined)

rep.intent <- felm(rep.intent.place ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education)  + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined)

rep.vote <- felm(voted.for.rep ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education)  + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined)

stargazer(rep.approval,rep.ideology,rep.party,rep.intent,rep.vote,keep=c('congruence'),dep.var.labels=c('Approval Place','Ideology Place','Recognize Name','Early Vote Intention','Voted for Rep'),covariate.labels=c('Congruence'),notes=c('Robust Standard Errors, Clustered by County-District'),star.cutoffs=c(0.05),notes.align='l')

#######
###Main Effects (Removing Counties with Newspaper Closure)
#######
rep.approval.noclosure <- felm(rep.approval.place ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education)  + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined,subset=which(is.na(final.combined$closure)))

rep.ideology.noclosure <- felm(rep.incumbent.ideology ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education)  + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined,subset=which(is.na(final.combined$closure)))

rep.party.noclosure <- felm(rep.party.place ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education)  + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined,subset=which(is.na(final.combined$closure)))

rep.intent.noclosure <- felm(rep.intent.place ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education)  + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined,subset=which(is.na(final.combined$closure)))

rep.vote.noclosure <- felm(voted.for.rep ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education)  + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined,subset=which(is.na(final.combined$closure)))

stargazer(rep.approval.noclosure,rep.ideology.noclosure,rep.party.noclosure,rep.intent.noclosure,rep.vote.noclosure,keep=c('congruence'),dep.var.labels=c('Approval Place','Ideology Place','Recognize Name','Early Vote Intention','Voted for Rep'),covariate.labels=c('Congruence'),notes=c('Robust Standard Errors, Clustered by County-District'),star.cutoffs=c(0.05),notes.align='l')

#######
###Shifts in DV From Typical Change in Congruence
#######
comparison.1 <- felm(congruence ~ factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined,subset=which(!is.na(rep.approval.place)))
comparison.2 <- felm(congruence ~ factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined,subset=which(!is.na(rep.incumbent.ideology)))
comparison.3 <- felm(congruence ~ factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined,subset=which(!is.na(rep.party.place)))
comparison.4 <- felm(congruence ~ factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined,subset=which(!is.na(rep.intent.place)))
comparison.5 <- felm(congruence ~ factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined,subset=which(!is.na(voted.for.rep)))

rep.approval$coefficients[1]*sd(comparison.1$residuals)
rep.ideology$coefficients[1]*sd(comparison.2$residuals)
rep.party$coefficients[1]*sd(comparison.3$residuals)
rep.intent$coefficients[1]*sd(comparison.4$residuals)
rep.vote$coefficients[1]*sd(comparison.5$residuals)

#######
###Heterogeneity by Political Interest
#######
rep.approval.interest <- felm(rep.approval.place ~ congruence*I(low.interest==1) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined)

rep.ideology.interest <- felm(rep.incumbent.ideology ~ congruence*I(low.interest==1) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined)

rep.party.interest <- felm(rep.party.place ~ congruence*I(low.interest==1) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined)

rep.intent.interest <- felm(rep.intent.place ~ congruence*I(low.interest==1) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined)

rep.vote.interest <- felm(voted.for.rep ~ congruence*I(low.interest==1) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined)

stargazer(rep.approval.interest,rep.ideology.interest,rep.party.interest,rep.intent.interest,rep.vote.interest,keep=c('congruence','low.interest'),dep.var.labels=c('Approval Place','Ideology Place','Recognize Name','Early Vote Intention','Voted for Rep'),covariate.labels=c('Congruence','Low Interest','Congruence*Low Interest'),notes=c('Robust Standard Errors, Clustered by County-District'),star.cutoffs=c(0.05),notes.align='l')

#######
###Comparison to ANES (Coefficients & SE from Snyder and Stromberg 2010)
#######

#Approval Diff: All
diff.approval <- rep.approval$coefficients[1] - .19
diff.approval.se <- sqrt(((.07)^2 + (rep.approval$cse[1])^2))
diff.approval; diff.approval + 1.96*diff.approval.se; diff.approval-1.96*diff.approval.se

#Ideology Diff: All
diff.ideology <- rep.ideology$coefficients[1] - .3
diff.ideology.se <- sqrt(((.1)^2 + (rep.ideology$cse[1])^2))
diff.ideology; diff.ideology + 1.96*diff.ideology.se; diff.ideology-1.96*diff.ideology.se

#Ideology Diff: Post 2010
rep.ideology.2010 <- felm(rep.incumbent.ideology ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined,subset=which(year > 2008))
diff.ideology.2010 <- .3 - rep.ideology.2010$coefficients[1]
diff.ideology.2010.se <- sqrt(((.1)^2 + (rep.ideology.2010$cse[1])^2))
diff.ideology.2010; diff.ideology.2010 + 1.96*diff.ideology.2010.se; diff.ideology.2010-1.96*diff.ideology.2010.se

#More Recent Comparisons within CCES Time Window
cces.coef.difference.3 <- function(model){
	
	post.coef <- model$coefficients[37]
	post.se <- sqrt(model$clustervcv[37,37])
	post.lower <- post.coef - 1.96*post.se
	post.upper <- post.coef + 1.96*post.se
	
	mid.coef <- model$coefficients[36]
	mid.se <- sqrt(model$clustervcv[36,36])
	mid.lower <- mid.coef - 1.96*mid.se
	mid.upper <- mid.coef + 1.96*mid.se
	
	pre.coef <- model$coefficients[35]
	pre.se <- sqrt(model$clustervcv[35,35])
	pre.lower <- pre.coef - 1.96*pre.se
	pre.upper <- pre.coef + 1.96*pre.se
	
	difference.coef <- post.coef - pre.coef
	diff.se <- sqrt(model$clustervcv[35,35] + model$clustervcv[37,37] - model$clustervcv[35,37])
	diff.lower <- difference.coef-1.96*diff.se
	diff.upper <- difference.coef+1.96*diff.se
	
	difference.coef.2 <- mid.coef - pre.coef 
	diff.se.2 <- sqrt(model$clustervcv[35,35] + model$clustervcv[36,36] - model$clustervcv[35,36])
	diff.lower.2 <- difference.coef.2-1.96*diff.se.2
	diff.upper.2 <- difference.coef.2+1.96*diff.se.2
	
	difference.coef.3 <- post.coef - mid.coef
	diff.se.3 <- sqrt(model$clustervcv[36,36] + model$clustervcv[37,37] - model$clustervcv[36,37])
	diff.lower.3 <- difference.coef.3 - 1.96*diff.se.3
	diff.upper.3 <- difference.coef.3 + 1.96*diff.se.3
	
	pre.output <- c(pre.coef,pre.se,pre.lower,pre.upper)
	mid.output <- c(mid.coef,mid.se,mid.lower,mid.upper)
	post.output <- c(post.coef,post.se,post.lower,post.upper)
	diff1.output <- c(difference.coef,diff.se,diff.lower,diff.upper)
	diff2.output <- c(difference.coef.2,diff.se.2,diff.lower.2,diff.upper.2)
	diff3.output <- c(difference.coef.3,diff.se.3,diff.lower.3,diff.upper.3)

	output.frame <- rbind.data.frame(pre.output,mid.output,post.output,diff1.output,diff2.output,diff3.output)
	output.frame$timing <- c('pre','mid','post','pre.post.diff','pre.mid.diff','mid.post.diff')
	names(output.frame) <- c('coef','se','lower','upper','timing')
	return(output.frame)
}

rep.approval.diff.cces.3 <- felm(rep.approval.place ~ congruence:factor(year.bin.3) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined)
approval.diff.3 <- cces.coef.difference.3(rep.approval.diff.cces.3)

rep.ideology.diff.cces.3 <- felm(rep.incumbent.ideology ~ congruence:factor(year.bin.3) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education)+ party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined)
ideology.diff.3 <- cces.coef.difference.3(rep.ideology.diff.cces.3)

rep.party.diff.cces.3 <- felm(rep.party.place ~ congruence:factor(year.bin.3) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined)
party.diff.3 <- cces.coef.difference.3(rep.party.diff.cces.3)

rep.intent.diff.cces.3 <- felm(rep.intent.place ~ congruence:factor(year.bin.3) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined)
intent.diff.3 <- cces.coef.difference.3(rep.intent.diff.cces.3)

rep.vote.diff.cces.3 <- felm(voted.for.rep ~ congruence:factor(year.bin.3) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined)
vote.diff.3 <- cces.coef.difference.3(rep.vote.diff.cces.3)

coef.pre.rows <- c(approval.diff.3$coef[1],ideology.diff.3$coef[1],party.diff.3$coef[1],intent.diff.3$coef[1],vote.diff.3$coef[1])
se.pre.rows <- c(approval.diff.3$se[1],ideology.diff.3$se[1],party.diff.3$se[1],intent.diff.3$se[1],vote.diff.3$se[1])

coef.mid.rows <- c(approval.diff.3$coef[2],ideology.diff.3$coef[2],party.diff.3$coef[2],intent.diff.3$coef[2],vote.diff.3$coef[2])
se.mid.rows <- c(approval.diff.3$se[2],ideology.diff.3$se[2],party.diff.3$se[2],intent.diff.3$se[2],vote.diff.3$se[2])

coef.post.rows <- c(approval.diff.3$coef[3],ideology.diff.3$coef[3],party.diff.3$coef[3],intent.diff.3$coef[3],vote.diff.3$coef[3])
se.post.rows <- c(approval.diff.3$se[3],ideology.diff.3$se[3],party.diff.3$se[3],intent.diff.3$se[3],vote.diff.3$se[3])

coef.diff.rows <- c(approval.diff.3$coef[4],ideology.diff.3$coef[4],party.diff.3$coef[4],intent.diff.3$coef[4],vote.diff.3$coef[4])
se.diff.rows <- c(approval.diff.3$se[4],ideology.diff.3$se[4],party.diff.3$se[4],intent.diff.3$se[4],vote.diff.3$se[4])

coef.diff.rows.2 <- c(approval.diff.3$coef[5],ideology.diff.3$coef[5],party.diff.3$coef[5],intent.diff.3$coef[5],vote.diff.3$coef[5])
se.diff.rows.2 <- c(approval.diff.3$se[5],ideology.diff.3$se[5],party.diff.3$se[5],intent.diff.3$se[5],vote.diff.3$se[5])

coef.diff.rows.3 <- c(approval.diff.3$coef[6],ideology.diff.3$coef[6],party.diff.3$coef[6],intent.diff.3$coef[6],vote.diff.3$coef[6])
se.diff.rows.3 <- c(approval.diff.3$se[6],ideology.diff.3$se[6],party.diff.3$se[6],intent.diff.3$se[6],vote.diff.3$se[6])

combined.output.frame <- rbind.data.frame(coef.pre.rows,se.pre.rows,coef.mid.rows,se.mid.rows,coef.post.rows,se.post.rows,coef.diff.rows.2,se.diff.rows.2,coef.diff.rows,se.diff.rows,coef.diff.rows.3,se.diff.rows.3)
names(combined.output.frame) <- c('Approval','Ideology','Party','Intent','Vote')

rownames(combined.output.frame) <- c('(1) 2006-2009','X1','(2) 2010-2013','X2','(3) 2014-2017','X3','(2) - (1)','X4','(3) - (1)','X5','(3) - (2)','X6')
xtable(combined.output.frame,digits=3)

#Year by Year for Approval Outcome
rep.approval.2006 <- felm(rep.approval.place ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined,subset=which(year=='2006'))

#Drop Income because it is unavailable
rep.approval.2007 <- felm(rep.approval.place ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined,subset=which(year=='2007'))

rep.approval.2008 <- felm(rep.approval.place ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined,subset=which(year=='2008'))

rep.approval.2009 <- felm(rep.approval.place ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined,subset=which(year=='2009'))

rep.approval.2010 <- felm(rep.approval.place ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined,subset=which(year=='2010'))

rep.approval.2012 <- felm(rep.approval.place ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined,subset=which(year=='2012'))

#Drop income this year because it is unavailable
rep.approval.2013 <- felm(rep.approval.place ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year ,data=final.combined,subset=which(year=='2013'))

rep.approval.2014 <- felm(rep.approval.place ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined,subset=which(year=='2014'))

rep.approval.2015 <- felm(rep.approval.place ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined,subset=which(year=='2015'))

rep.approval.2016 <- felm(rep.approval.place ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined,subset=which(year=='2016'))

#Drop income this year because it is unavailable
rep.approval.2017 <- felm(rep.approval.place ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined,subset=which(year=='2017'))

year.labs.new <- c('2006','2007','2008','2009','2010','2011','2012','2013','2014','2015','2016','2017')
approve.coefficients.new <- c(rep.approval.2006$coefficients[1],rep.approval.2007$coefficients[1],rep.approval.2008$coefficients[1],rep.approval.2009$coefficients[1],rep.approval.2010$coefficients[1],NA,rep.approval.2012$coefficients[1],rep.approval.2013$coefficients[1],rep.approval.2014$coefficients[1],rep.approval.2015$coefficients[1],rep.approval.2016$coefficients[1],rep.approval.2017$coefficients[1])
approve.ses.new <- c(rep.approval.2006$cse[1],rep.approval.2007$cse[1],rep.approval.2008$cse[1],rep.approval.2009$cse[1],rep.approval.2010$cse[1],NA,rep.approval.2012$cse[1],rep.approval.2013$cse[1],rep.approval.2014$cse[1],rep.approval.2015$cse[1],rep.approval.2016$cse[1],rep.approval.2017$cse[1])
approve.frame <- cbind.data.frame(approve.coefficients.new,approve.ses.new)
approve.frame$upper <- approve.frame$approve.coefficients.new + 2*approve.frame$approve.ses.new
approve.frame$lower<- approve.frame$approve.coefficients.new - 2*approve.frame$approve.ses.new


#setEPS()
#postscript(file='fig1.eps',height=4,width=6)
pdf(file='fig1.pdf',height=4,width=6)
par(mar=c(4,4,1,1))
plot(x=1:length(year.labs.new),y=approve.coefficients.new,ylim=c(-.05,.35),pch=16,las=1,xlim=c(-3,13),xaxt='n',ylab='',xlab='Year',cex=1.5,,main='')
segments(x0=1:length(year.labs.new),y0=approve.frame$upper,y1=approve.frame$lower,lwd=4)

points(x=-2,y=0.19,col='black',cex=1.5,pch=16)
segments(x0=-2,y0=.05,y1=.33,lwd=4,col='black')
abline(v=-.5,lty=2)
axis(side=1,at=1:length(year.labs.new),labels=year.labs.new,cex.axis=.9)
axis(side=1,at=-2,labels='1982-2004',cex.axis=.9)
dev.off()


#Year by Year for Ideology Outcome
rep.ideology.2006 <- felm(rep.incumbent.ideology ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined,subset=which(year=='2006'))
#Exclude Income for 2007
rep.ideology.2007 <- felm(rep.incumbent.ideology ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined,subset=which(year=='2007'))
rep.ideology.2008 <- felm(rep.incumbent.ideology ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined,subset=which(year=='2008'))
rep.ideology.2010 <- felm(rep.incumbent.ideology ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined,subset=which(year=='2010'))
rep.ideology.2011 <- felm(rep.incumbent.ideology ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined,subset=which(year=='2011'))
rep.ideology.2012 <- felm(rep.incumbent.ideology ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined,subset=which(year=='2012'))
rep.ideology.2014 <- felm(rep.incumbent.ideology ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined,subset=which(year=='2014'))
rep.ideology.2015 <- felm(rep.incumbent.ideology ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined,subset=which(year=='2015'))
rep.ideology.2016 <- felm(rep.incumbent.ideology ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined,subset=which(year=='2016'))
rep.ideology.2017 <- felm(rep.incumbent.ideology ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined,subset=which(year=='2017'))

ideology.coefficients.new <- c(rep.ideology.2006$coefficients[1],rep.ideology.2007$coefficients[1],rep.ideology.2008$coefficients[1],NA,rep.ideology.2010$coefficients[1],rep.ideology.2011$coefficients[1],rep.ideology.2012$coefficients[1],NA,rep.ideology.2014$coefficients[1],rep.ideology.2015$coefficients[1],rep.ideology.2016$coefficients[1],rep.ideology.2017$coefficients[1])
ideology.ses.new <- c(rep.ideology.2006$cse[1],rep.ideology.2007$cse[1],rep.ideology.2008$cse[1],NA,rep.ideology.2010$cse[1],rep.ideology.2011$cse[1],rep.ideology.2012$cse[1],NA,rep.ideology.2014$cse[1],rep.ideology.2015$cse[1],rep.ideology.2016$cse[1],rep.ideology.2017$cse[1])

ideology.frame <- cbind.data.frame(ideology.coefficients.new,ideology.ses.new)
ideology.frame$upper <- ideology.frame$ideology.coefficients.new + 2*ideology.frame$ideology.ses.new
ideology.frame$lower<- ideology.frame$ideology.coefficients.new - 2*ideology.frame$ideology.ses.new

#setEPS()
#postscript(file='fig2.eps',height=4,width=6)
pdf(file='fig2.pdf',height=4,width=6)
par(mar=c(4,4,1,1))
plot(x=1:length(year.labs.new),y=ideology.coefficients.new,ylim=c(-.05,.5),pch=16,las=1,xlim=c(-3,13),xaxt='n',ylab='',xlab='Year',cex=1.5,,main='')
segments(x0=1:length(year.labs.new),y0=ideology.frame$upper,y1=ideology.frame$lower,lwd=4)

points(x=-2,y=0.30,col='black',cex=1.5,pch=16)
segments(x0=-2,y0=.1,y1=.5,lwd=4,col='black')
abline(v=-.5,lty=2)
axis(side=1,at=1:length(year.labs.new),labels=year.labs.new,cex.axis=.9)
axis(side=1,at=-2,labels='1982-2004',cex.axis=.9)
dev.off()


####
##Relationship between Congruence and Outcomes
####

#Bivariate Relationship

rep.approval.bivariate <- lm(rep.approval.place ~ congruence + factor(year),data=final.combined)
rep.ideology.bivariate <- lm(rep.incumbent.ideology ~ congruence + factor(year),data=final.combined)
rep.party.bivariate <- lm(rep.party.place ~ congruence + factor(year),data=final.combined)
rep.intent.bivariate <- lm(rep.intent.place ~ congruence + factor(year),data=final.combined)
rep.vote.bivariate <- lm(voted.for.rep ~ congruence + factor(year),data=final.combined)

stargazer(rep.approval.bivariate,rep.ideology.bivariate,rep.party.bivariate,rep.intent.bivariate,rep.vote.bivariate,keep=c('congruence','year'),dep.var.labels=c('Approval Place','Ideology Place','Recognize Name','Early Vote Intention','Voted for Rep'),notes.align='l',star.cutoffs=c(0.05))


#Binned Measure of Congruence
rep.approval.binned <- felm(rep.approval.place ~ factor(congruence.5bins) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.district.year,data=final.combined)

rep.ideology.binned <- felm(rep.incumbent.ideology ~ factor(congruence.5bins) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.district.year,data=final.combined)

rep.party.binned <- felm(rep.party.place ~ factor(congruence.5bins) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.district.year,data=final.combined)

rep.intent.binned <- felm(rep.intent.place ~ factor(congruence.5bins) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.district.year,data=final.combined)

rep.vote.binned <- felm(voted.for.rep ~ factor(congruence.5bins) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.district.year,data=final.combined)

stargazer(rep.approval.binned,rep.ideology.binned,rep.party.binned,rep.intent.binned,rep.vote.binned,keep=c('congruence'),dep.var.labels=c('Approval Place','Ideology Place','Recognize Name','Early Vote Intention','Voted for Rep'),notes=c('Robust Standard Errors, Clustered by County-District'),star.cutoffs=c(0.05),notes.align='l',covariate.labels=c('Congruence Bin 2','Congruence Bin 3','Congruence Bin 4','Congruence Bin 5'))

####
#Interaction With Political Interest Scale
###

#4-pt Scale

rep.approval.interest.4cat <- felm(rep.approval.place ~ congruence*factor(political.interest) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined)

rep.ideology.interest.4cat <- felm(rep.incumbent.ideology ~ congruence*factor(political.interest) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined)

rep.party.interest.4cat <- felm(rep.party.place ~ congruence*factor(political.interest) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined)

rep.intent.interest.4cat <- felm(rep.intent.place ~ congruence*factor(political.interest) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined)

rep.vote.interest.4cat <- felm(voted.for.rep ~ congruence*factor(political.interest) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined)

stargazer(rep.approval.interest.4cat,rep.ideology.interest.4cat,rep.party.interest.4cat,rep.intent.interest.4cat,rep.vote.interest.4cat,keep=c('congruence','interest'),dep.var.labels=c('Approval Place','Ideology Place','Recognize Name','Early Vote Intention','Voted for Rep'),notes=c('Robust Standard Errors, Clustered by County-District'),star.cutoffs=c(0.05),notes.align='l',covariate.labels=c('Congruence','Political Interest-2','Political Interest-3','Political Interest-4','Congruence*Political Interest-2','Congruence*Political Interest-3','Congruence*Political Interest-4'))

####
##Interaction With Newspaper Readership When Available
####

rep.approval.newspaper <- felm(rep.approval.place ~ congruence*factor(newspaper.use) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined)

rep.ideology.newspaper <- felm(rep.incumbent.ideology ~ congruence*factor(newspaper.use) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined)

rep.party.newspaper <- felm(rep.party.place ~ congruence*factor(newspaper.use) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined)

rep.intent.newspaper <- felm(rep.intent.place ~ congruence*factor(newspaper.use) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined)

rep.vote.newspaper <- felm(voted.for.rep ~ congruence*factor(newspaper.use) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | state.county.district.year,data=final.combined)

stargazer(rep.approval.newspaper,rep.ideology.newspaper,rep.party.newspaper,rep.intent.newspaper,rep.vote.newspaper,keep=c('congruence','newspaper'),dep.var.labels=c('Approval Place','Ideology Place','Recognize Name','Early Vote Intention','Voted for Rep'),notes=c('Robust Standard Errors, Clustered by County-District'),star.cutoffs=c(0.05),notes.align='l',covariate.labels=c('Congruence','Newspaper Use','Congruence*Newspaper Use'))

###
#All over time comparisons
###

#Approval
rep.approval.baseline1 <- felm(rep.approval.place ~ congruence | year + incumbent.term.fe | 0 | county_fips,data=final.combined)
rep.approval.baseline1.ss <- .21
rep.approval.baseline1.ss.se <- .05
rep.approval.baseline1.diff <- rep.approval.baseline1.ss - rep.approval.baseline1$coefficients[1]
rep.approval.baseline1.diff.se <- sqrt(((rep.approval.baseline1.ss.se)^2 + (rep.approval.baseline1$cse[1])^2))


rep.approval.baseline2 <- felm(rep.approval.place ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match  + freshman + tenure + majority.party + power.committee + power.position + death + retired + incumbent.running + unopposed + margin+ population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education| state.year + incumbent.term.fe | 0 | county_fips,data=final.combined)
rep.approval.baseline2.ss <- .20
rep.approval.baseline2.ss.se <- .06
rep.approval.baseline2.diff <- rep.approval.baseline2.ss - rep.approval.baseline2$coefficients[1]
rep.approval.baseline2.diff.se <- sqrt(((rep.approval.baseline2.ss.se)^2 + (rep.approval.baseline2$cse[1])^2))


rep.approval.withindistrict <- felm(rep.approval.place ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education)  + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | county_fips,data=final.combined)
rep.approval.withindistrict.ss <- .19
rep.approval.withindistrict.ss.se <- .07
rep.approval.withindistrict.diff <- rep.approval.withindistrict.ss - rep.approval.withindistrict$coefficients[1]
rep.approval.withindistrict.diff.se <- sqrt(((rep.approval.withindistrict.ss.se)^2 + (rep.approval.withindistrict$cse[1])^2))


rep.approval.redistrict <- felm(rep.incumbent.ideology ~ congruence.redistrict + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education)  + party.match  + freshman + tenure + majority.party + power.committee + power.position + death + retired + incumbent.running + unopposed + margin | county_fips + state.year | 0 | county_fips,data=final.combined)
rep.approval.redistrict.ss <- .29
rep.approval.redistrict.ss.se <- .09
rep.approval.redistrict.diff <- rep.approval.redistrict.ss - rep.approval.redistrict$coefficients[1]
rep.approval.redistrict.diff.se <- sqrt(((rep.approval.redistrict.ss.se)^2 + (rep.approval.redistrict$cse[1])^2))

coef.ss <- c(rep.approval.baseline1.ss,rep.approval.baseline2.ss,rep.approval.withindistrict.ss,rep.approval.redistrict.ss)
se.ss <- c(rep.approval.baseline1.ss.se,rep.approval.baseline2.ss.se,rep.approval.withindistrict.ss.se,rep.approval.redistrict.ss.se)
approval.ss <- cbind.data.frame(coef.ss,se.ss)
approval.ss$upper <- approval.ss[,1] + 2*approval.ss[,2]
approval.ss$lower <- approval.ss[,1] - 2*approval.ss[,2]

coef.current <- c(rep.approval.baseline1$coefficients[1],rep.approval.baseline2$coefficients[1],rep.approval.withindistrict$coefficients[1],rep.approval.redistrict$coefficients[1])
se.current <- c(rep.approval.baseline1$cse[1],rep.approval.baseline2$cse[1],rep.approval.withindistrict$cse[1],rep.approval.redistrict$cse[1])
approval.current <- cbind.data.frame(coef.current,se.current)
approval.current$upper <- approval.current[,1] + 2*approval.current[,2]
approval.current$lower <- approval.current[,1] - 2*approval.current[,2]


coef.diff <- c(rep.approval.baseline1.diff,rep.approval.baseline2.diff,rep.approval.withindistrict.diff,rep.approval.redistrict.diff)
se.diff <- c(rep.approval.baseline1.diff.se,rep.approval.baseline2.diff.se,rep.approval.withindistrict.diff.se,rep.approval.redistrict.diff.se)
approval.diff <- cbind.data.frame(coef.diff,se.diff)
approval.diff$upper <- approval.diff[,1] + 2*approval.diff[,2]
approval.diff$lower <- approval.diff[,1] - 2*approval.diff[,2]

pdf(file='approval-allspecification-comparison.pdf',height=8,width=7)
par(mar=c(4.1,6,4,6))
plot(y=4:1,x=coef.current,pch=16,cex=2,xlim=c(-.1,.5),ylab='',yaxt='n',xlab='Effect of Congruence',col='black',ylim=c(.7,4.3),main="Effect of Congruence on Approval Placement")
abline(v=0,lty=2)
segments(y0=4:1,x0=approval.current$upper,x1=approval.current$lower,lwd=4)
points(y=4:1+.25,x=coef.ss,pch=16,cex=2,col='gray40')
segments(y0=4:1+.25,x0=approval.ss$upper,x1=approval.ss$lower,lwd=4,col='gray40')
points(y=4:1-.25,x=coef.diff,pch=16,cex=2,col='gray70')
segments(y0=4:1-.25,x0=approval.diff$upper,x1=approval.diff$lower,lwd=4,col='gray70')
abline(h=c(1.5,2.5,3.5))
axis(side=2,at=c(4.25,4,3.75,3.25,3,2.75,2.25,2,1.75,1.25,1,.75),labels=c('(1982-2004)','(2006-2017)','Difference','(1982-2004)','(2006-2017)','Difference','(1982-2004)','(2006-2017)','Difference','(1982-2004)','(2006-2017)','Difference'),las=1)
axis(side=4,at=c(4,3,2,1),labels=c('Baseline','W/ Controls','Within-District','Redistricting'),las=1,tick=FALSE,line=-.75)
dev.off()

#Ideology
rep.ideology.baseline1 <- felm(rep.incumbent.ideology ~ congruence | year + incumbent.term.fe | 0 | county_fips,data=final.combined)
rep.ideology.baseline1.ss <- .22
rep.ideology.baseline1.ss.se <- .09
rep.ideology.baseline1.diff <- rep.ideology.baseline1.ss - rep.ideology.baseline1$coefficients[1]
rep.ideology.baseline1.diff.se <- sqrt(((rep.ideology.baseline1.ss.se)^2 + (rep.ideology.baseline1$cse[1])^2))


rep.ideology.baseline2 <- felm(rep.incumbent.ideology ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match  + freshman + tenure + majority.party + power.committee + power.position + death + retired + incumbent.running + unopposed + margin+ population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education| state.year + incumbent.term.fe | 0 | county_fips,data=final.combined)
rep.ideology.baseline2.ss <- .25
rep.ideology.baseline2.ss.se <- .09
rep.ideology.baseline2.diff <- rep.ideology.baseline2.ss - rep.ideology.baseline2$coefficients[1]
rep.ideology.baseline2.diff.se <- sqrt(((rep.ideology.baseline2.ss.se)^2 + (rep.ideology.baseline2$cse[1])^2))

rep.ideology.withindistrict <- felm(rep.incumbent.ideology ~ congruence + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education)  + party.match + population.logged + female.share + black.share + under20.share + over65.share + log.density + log.median.household.income + share.11.education + share.12.education + share.more.education | state.district.year | 0 | county_fips,data=final.combined)
rep.ideology.withindistrict.ss <- .30
rep.ideology.withindistrict.ss.se <- .1
rep.ideology.withindistrict.diff <- rep.ideology.withindistrict.ss - rep.ideology.withindistrict$coefficients[1]
rep.ideology.withindistrict.diff.se <- sqrt(((rep.ideology.withindistrict.ss.se)^2 + (rep.ideology.withindistrict$cse[1])^2))

rep.ideology.redistrict <- felm(rep.incumbent.ideology ~ congruence.redistrict + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education)  + party.match  + freshman + tenure + majority.party + power.committee + power.position + death + retired + incumbent.running + unopposed + margin | county_fips + state.year | 0 | county_fips,data=final.combined)
rep.ideology.redistrict.ss <- .22
rep.ideology.redistrict.ss.se <- .09
rep.ideology.redistrict.diff <- rep.ideology.redistrict.ss - rep.ideology.redistrict$coefficients[1]
rep.ideology.redistrict.diff.se <- sqrt(((rep.ideology.redistrict.ss.se)^2 + (rep.ideology.redistrict$cse[1])^2))

coef.ss <- c(rep.ideology.baseline1.ss,rep.ideology.baseline2.ss,rep.ideology.withindistrict.ss,rep.ideology.redistrict.ss)
se.ss <- c(rep.ideology.baseline1.ss.se,rep.ideology.baseline2.ss.se,rep.ideology.withindistrict.ss.se,rep.ideology.redistrict.ss.se)
ideology.ss <- cbind.data.frame(coef.ss,se.ss)
ideology.ss$upper <- ideology.ss[,1] + 2*ideology.ss[,2]
ideology.ss$lower <- ideology.ss[,1] - 2*ideology.ss[,2]

coef.current <- c(rep.ideology.baseline1$coefficients[1],rep.ideology.baseline2$coefficients[1],rep.ideology.withindistrict$coefficients[1],rep.ideology.redistrict$coefficients[1])
se.current <- c(rep.ideology.baseline1$cse[1],rep.ideology.baseline2$cse[1],rep.ideology.withindistrict$cse[1],rep.ideology.redistrict$cse[1])
ideology.current <- cbind.data.frame(coef.current,se.current)
ideology.current$upper <- ideology.current[,1] + 2*ideology.current[,2]
ideology.current$lower <- ideology.current[,1] - 2*ideology.current[,2]

coef.diff <- c(rep.ideology.baseline1.diff,rep.ideology.baseline2.diff,rep.ideology.withindistrict.diff,rep.ideology.redistrict.diff)
se.diff <- c(rep.ideology.baseline1.diff.se,rep.ideology.baseline2.diff.se,rep.ideology.withindistrict.diff.se,rep.ideology.redistrict.diff.se)
ideology.diff <- cbind.data.frame(coef.diff,se.diff)
ideology.diff$upper <- ideology.diff[,1] + 2*ideology.diff[,2]
ideology.diff$lower <- ideology.diff[,1] - 2*ideology.diff[,2]

pdf(file='ideology-allspecification-comparison.pdf',height=8,width=7)
par(mar=c(4.1,6,4,6))
plot(y=4:1,x=coef.current,pch=16,cex=2,xlim=c(-.125,.5),ylab='',yaxt='n',xlab='Effect of Congruence',col='black',ylim=c(.7,4.3),main="Effect of Congruence on Ideology Placement")
abline(v=0,lty=2)
segments(y0=4:1,x0=ideology.current$upper,x1=ideology.current$lower,lwd=4)
points(y=4:1+.25,x=coef.ss,pch=16,cex=2,col='gray40')
segments(y0=4:1+.25,x0=ideology.ss$upper,x1=ideology.ss$lower,lwd=4,col='gray40')
points(y=4:1-.25,x=coef.diff,pch=16,cex=2,col='gray70')
segments(y0=4:1-.25,x0=ideology.diff$upper,x1=ideology.diff$lower,lwd=4,col='gray70')
abline(h=c(1.5,2.5,3.5))
axis(side=2,at=c(4.25,4,3.75,3.25,3,2.75,2.25,2,1.75,1.25,1,.75),labels=c('(1982-2004)','(2006-2017)','Difference','(1982-2004)','(2006-2017)','Difference','(1982-2004)','(2006-2017)','Difference','(1982-2004)','(2006-2017)','Difference'),las=1)
axis(side=4,at=c(4,3,2,1),labels=c('Baseline','W/ Controls','Within-District','Redistricting'),las=1,tick=FALSE,line=-.75)
dev.off()

###
#Redistricting Overall
###
rep.approval.redistrict <- felm(rep.approval.place ~ congruence.redistrict + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education)  + party.match  + freshman + tenure + majority.party + power.committee + power.position + death + retired + incumbent.running + unopposed + margin | county_fips + state.year | 0 | county_fips,data=final.combined)

rep.ideology.redistrict <- felm(rep.incumbent.ideology ~ congruence.redistrict + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education)  + party.match  + freshman + tenure + majority.party + power.committee + power.position + death + retired + incumbent.running + unopposed + margin | county_fips + state.year | 0 | county_fips,data=final.combined)

rep.party.redistrict <- felm(rep.party.place ~ congruence.redistrict + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education)  + party.match  + freshman + tenure + majority.party + power.committee + power.position + death + retired + incumbent.running + unopposed + margin | county_fips + state.year | 0 | county_fips,data=final.combined)

rep.intent.redistrict <- felm(rep.intent.place ~ congruence.redistrict + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education)  + party.match  + freshman + tenure + majority.party + power.committee + power.position + death + retired + incumbent.running + unopposed + margin | county_fips + state.year | 0 | county_fips,data=final.combined)

rep.vote.redistrict <- felm(voted.for.rep ~ congruence.redistrict + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education)  + party.match  + freshman + tenure + majority.party + power.committee + power.position + death + retired + incumbent.running + unopposed + margin | county_fips + state.year | 0 | county_fips,data=final.combined)

stargazer(rep.approval.redistrict,rep.ideology.redistrict,rep.party.redistrict,rep.intent.redistrict,rep.vote.redistrict,keep=c('congruence.redistrict'),dep.var.labels=c('Approval Place','Ideology Place','Recognize Name','Early Vote Intention','Voted for Rep'),covariate.labels=c('Congruence'),notes=c('Robust Standard Errors, Clustered by County'),star.cutoffs=c(0.05),notes.align='l')

###
#Redistricting Political Interest Interaction
###
rep.approval.redistrict.interest <- felm(rep.approval.place ~ congruence.redistrict*I(low.interest==1) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education)  + party.match  + freshman + tenure + majority.party + power.committee + power.position + death + retired + incumbent.running + unopposed + margin | county_fips + state.year | 0 | county_fips,data=final.combined)

rep.ideology.redistrict.interest <- felm(rep.incumbent.ideology ~ congruence.redistrict*I(low.interest==1) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education)  + party.match  + freshman + tenure + majority.party + power.committee + power.position + death + retired + incumbent.running + unopposed + margin | county_fips + state.year | 0 | county_fips,data=final.combined)

rep.party.redistrict.interest <- felm(rep.party.place ~ congruence.redistrict*I(low.interest==1) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education)  + party.match  + freshman + tenure + majority.party + power.committee + power.position + death + retired + incumbent.running + unopposed + margin | county_fips + state.year | 0 | county_fips,data=final.combined)

rep.intent.redistrict.interest <- felm(rep.intent.place ~ congruence.redistrict*I(low.interest==1) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education)  + party.match  + freshman + tenure + majority.party + power.committee + power.position + death + retired + incumbent.running + unopposed + margin | county_fips + state.year | 0 | county_fips,data=final.combined)

rep.vote.redistrict.interest <- felm(voted.for.rep ~ congruence.redistrict*I(low.interest==1) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education)  + party.match  + freshman + tenure + majority.party + power.committee + power.position + death + retired + incumbent.running + unopposed + margin | county_fips + state.year | 0 | county_fips,data=final.combined)

rep.vote.redistrict.interest <- felm(voted.for.rep ~ congruence.redistrict*I(low.interest==1) + factor(age.bin) + factor(gender) + factor(race) + factor(income) + factor(education) + party.match + freshman + tenure + majority.party  + power.committee + power.position + death + retired + incumbent.running + unopposed + margin | county_fips + state.year | 0 | county_fips,data=final.combined)

stargazer(rep.approval.redistrict.interest,rep.ideology.redistrict.interest,rep.party.redistrict.interest,rep.intent.redistrict.interest,rep.vote.redistrict.interest,keep=c('congruence.redistrict','interest'),dep.var.labels=c('Approval Place','Ideology Place','Recognize Name','Early Vote Intention','Voted for Rep'),covariate.labels=c('Congruence','Low-Interest','Congruence*Low-Interest'),notes=c('Robust Standard Errors, Clustered by County'),star.cutoffs=c(0.05),notes.align='l')